<?php
/**
 * Date: 2018/6/20
 * Time: 15:46
 * Author: zeronlee
 */

namespace App\Services\Vuls;
use App\Services\Email\Email;

class Distribute
{
    //默认权限角色
    public $defaultRoleId = 0;

    public function __construct () {
        $this->defaultRoleId = env('BUSINESSROLEID');
    }

    /**
     * 根据账号查询信息
     * @param string $name
     * @return array
     */
    public function getUser($name)
    {
        $userInfo=array();
        if (!empty($name)){
            ob_start();

            $userParameters["Account"] = $name;
            //TODO 配置时间
            $userParameters["StartDate"] = "2015-01-04 00:31:30";
            $userParameters["EndDate"] = "2020-01-04 18:31:30";
            $service = new Email();
            $service->setMethod('EHR_HRMService_GetEmpDirectory');
            $base_sys=json_decode($service->odsPost($userParameters),true);
            \Log::info('base sys ret is '.json_encode($base_sys));
            $base_info=$base_sys["MsgResponse"]["ResponseInfo"];

            if($base_info['Result']==1)
            {
                $base_data=$base_sys["MsgResponse"]["Data"]['NewDataSet'][0];
                unset($userParameters["Account"]);
                $userParameters["EmployeeID"] = $base_data['vcEmployeeID'];
                //员工联系信息
                $service->setMethod('EHR_HRMService_GetEmpMessageInfo');
                $connect_sys=json_decode($service->odsPost($userParameters),true);
                \Log::info('connect sys ret is '.json_encode($connect_sys));
                if($connect_sys["MsgResponse"]["ResponseInfo"]['Result']==1)
                {
                    $connect_data=$connect_sys["MsgResponse"]["Data"]['NewDataSet'][0];
                    $userInfo=array_merge($base_data,$connect_data);
                }else{
                    $userInfo=$base_data;
                }
            }
            ob_clean();
            ob_end_clean();
        }

        return  $userInfo;
    }

    /**
     * 授权默认角色
     * @param mixed $user
     */
    public function roleDefault ($user){

        $user->flushRoles([$this->defaultRoleId]);
    }
}